package com.v5ba.annotation.scan;

import com.v5ba.scan.OrgService;
import com.v5ba.scan.Supper;
import com.v5ba.scan.UserService;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 * @author yiren
 * @since 2022/10/19
 */
@Configuration
@ComponentScan(basePackages = {"com.v5ba.scan"}, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyExcludeFilter.class})
}, includeFilters ={
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyIncludeFilter.class})
})
public class MyScanConfiguration {
}
class MyIncludeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        ClassMetadata metadata = metadataReader.getClassMetadata();
        if (metadata.getInterfaceNames().length > 0) {
            for (String interfaceName : metadata.getInterfaceNames()) {
                if (Supper.class.getName().equals(interfaceName)){
                    return true;
                }
            }
        }
        return false;
    }
}
class MyExcludeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        ClassMetadata metadata = metadataReader.getClassMetadata();
        String className = metadata.getClassName();
        System.out.println("---------"+metadata.getClassName());
        if (OrgService.class.getName().equals(className)) {
            return true;
        }
        return false;
    }
}
